<template>
	<view class="comment">
		<nav-bar :title="navText"></nav-bar>

		<view class="card">
			
			<view class="rate-bar">
				<text style="color: #007AFF;">轻点评分</text>
				<uni-rate :value="4.6" @change="" :is-fill="false" :allowHalf="true" color="#ffca3e" />
			</view>
			
			<u-empty v-if="commentState === -1 && commentCount < 0" mode="wifi">
			</u-empty>

			<u-empty v-if="commentCount==0" mode="data" text="暂无评论">
			</u-empty>

			<block v-for="(commentVo,$index) in allComments" :key="commentVo.comment.id">

				<view style="flex-direction : row; display: flex;  margin: 10px 0;	width: 100%;">
					<!-- 用户头像-->
					<view style="width:50px; flex-shrink : 0;">
						<image :src="commentVo.user.img" class="img">
						</image>
					</view>

					<view style="flex: 1; display: flex; flex-direction : column;">
						<!-- 用户信息-->
						<view class="info">
							<text style="flex : 1; color : hotpink;">
								{{ commentVo.user.nickname }}
							</text>
							<text style="flex : 1; color : darkgray;font-size: 14px;">
								{{formatTime(commentVo.comment.time)}}
							</text>
						</view>

						<!-- 评论内容-->
						<view style="margin-bottom : 10px;">
							<!-- <text>{{commentVo.comment.content}}</text> -->
							<ex-text :title="commentVo.comment.content" :size="16"></ex-text>
						</view>

						<!-- 回复 -->
						<view class="reply" v-if="commentVo.list && commentVo.list.length>0">

							<block v-for="vo in commentVo.list" :key="vo.comment.id">
								<view style="display: flex; flex-direction: column;">
									<text v-if="vo.comment.communication">
										<text style="color: #4CD964;">
											{{vo.user.nickname}}
										</text>
										回复
										<text style="color: #F0AD4E;">
											{{vo.commUser.nickname}}
										</text>
										<text>:{{vo.comment.content}}</text>
									</text>
									<text v-else>
										<text style="color: #4CD964;">{{vo.user.nickname}}</text>
										<text>:{{vo.comment.content}}</text>
									</text>
								</view>

							</block>
						</view>
					</view>

				</view>
			</block>
		</view>

		<u-loading-icon :show="commentState==-2&&commentCount>0&&!isPull" text="加载中"></u-loading-icon>

		<u-divider text="没有多余评论了~" v-if="commentCount===allComments.length"></u-divider>


		<view class="used" :style="{height:commentHeight + 30 + 'px'}"></view>

		<view class="bottom" :style="{height:commentHeight + 30 + 'px'}">
			<view class="comment-bar" :style="{height:commentHeight + 20 + 'px'}">
				<u--textarea placeholder="来发一条友善的评论吧~" fixed :height="commentHeight" @linechange="lineChange"
					maxlength="-1" v-model="commentText" style="border-radius: 20px;">
				</u--textarea>
				<u-button type="primary" text="发送" hairline shape="circle" :style="{width:'20%',marginLeft:'10px'}"
					@click="sendComment">
				</u-button>
			</view>

		</view>
	</view>
</template>

<script>
	import utils from '@/static/js/utils.js'
	export default {
		data() {
			return {
				// 总评论数
				commentCount: -1,
				commentState: -3,
				// 所有评论
				allComments: [],
				commentPage: 1,
				commentPageSize: 10,
				bookId: -1,
				title: "",
				// 评论
				commentText: "",
				commentHeight: 40,
				isPull: true
			}
		},
		onLoad(option) {
			this.bookId = option.bookId
			this.title = option.title
			// uni.startPullDownRefresh()
			this.commentLoad(this.isPull)
		},
		computed: {
			navText() {
				if (this.commentCount > 0) {
					return `评论(${this.commentCount})`
				} else {
					return `评论`
				}
			}
		},
		onPullDownRefresh() {
			this.commentPage = 1
			// 下拉刷新
			this.isPull = true
			this.commentLoad(this.isPull)
		},

		onReachBottom() {
			this.isPull = false

			if (this.allComments.length === this.commentCount) {

				return
			}
			this.commentLoad(this.isPull)
		},

		methods: {
			

			sendComment() {

				if (!this.commentText || this.commentText === "") {
					return
				}

				console.log("comment", this.commentText)
				utils.http.get("/comment/publish", {
					content: this.commentText,
					bookId: this.bookId
				}).then(res => {
					if (res.status === 200) {
						// 发送成功
						this.commentText = ""
						uni.showToast({
							icon: "none",
							title: "成功"
						})
						uni.startPullDownRefresh()
					} else {
						uni.showToast({
							icon: "error",
							title: "评论失败"
						})
					}
				}).finally(e => {

				})
			},
			lineChange(e) {
				console.log("comment", e)
				if (e.detail.lineCount <= 2)
					this.commentHeight = e.detail.height
			},
			formatTime(time) {
				return utils.formatTime(time)
			},
			commentLoad(isPull = false) {


				if (this.commentState == -2) {
					return
				}

				// 加载状态
				this.commentState = -2

				if (isPull) {
					uni.startPullDownRefresh()
				}

				utils.http.get(
					"/comment/list", {
						bookId: this.bookId,
						page: this.commentPage,
						pageSize: this.commentPageSize
					}
				).then(res => {
					console.log(res.data)

					// 成功返回
					this.commentState = 0
					if (res.status === 200) {

						if (isPull) {
							this.allComments = []
						}
						res.data.list.forEach(vo => {
							vo.user.img = utils.http.baseUrl + vo.user.img
							this.allComments.push(vo)
						})

						this.commentCount = res.data.count
						this.commentPage = this.commentPage + 1
					}

				}).catch(error => {
					// 网络异常
					this.commentState = -1
					uni.showToast({
						icon: "none",
						title: "加载异常"
					})
				}).finally(() => {
					if (isPull) {
						uni.stopPullDownRefresh()
					}
				})
			}
		}
	}
</script>

<style>
	.comment {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		/* background-color: #fafafa; */
	}

	.comment .rate-bar {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 10px 5%;
		background-color: #F0F0F0;
		border-radius: 10px;
		margin-bottom: 10px;
		width: 90%;
	}

	.comment .card {
		display: flex;
		width: 90%;
		flex-direction: column;
		justify-content: center;
		margin: 10px 0;
	}

	.comment .img {
		width: 35px;
		height: 35px;
		border-radius: 50% 50%;
	}

	.comment .info {
		display: flex;
		flex-direction: column;
		height: 40px;
		margin-bottom: 7px;
		flex-shrink: 0;
	}

	.comment .used {
		width: 100%;
		background-color: white;
	}

	.comment .bottom {
		width: 100%;
		bottom: 0;
		left: 0;
		background-color: white;
		position: fixed;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		border-top: #C0C0C0 solid 0.5px;
		z-index: 999;
	}

	.comment .comment-bar {
		width: 95%;
		display: flex;
		flex-direction: row;
		align-items: flex-end;
		justify-content: center;
	}

	.comment .reply {
		display: flex;
		flex-direction: column;
		background-color: #f0f0f0;
		border-radius: 7px 7px;
		padding: 10px 5%;
		width: 90%;
	}

	.comment .bottom-bar {
		width: 100%;
	}

	text {
		word-break: break-all;
	}
</style>
